package cn.qihua.action;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.json.annotations.JSON;

import cn.qihua.model.BaseProduct;
import cn.qihua.service.IProductService;
import cn.qihua.util.DateUtils;
import cn.qihua.util.IPageAble;
import cn.qihua.util.RegexUtil;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author 米修
 * 
 */

public class ListProductAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3162420523636914919L;
	public IPageAble page;
	public String message;
	public Integer total;
	private boolean success = false;
	private IProductService productService;
	private List<BaseProduct> listProduct;
	public String query;

	// public String findAll() {
	// listBaseProduct = BaseProductService.findAll(page.getStart(),
	// page.getLimit());
	// this.setTotal(BaseProductService.getTotal());
	// this.setSuccess(true);
	// return SUCCESS;
	// }

	public String findByLostDate() {
		listProduct = productService.findByNow(7, page.getStart(),
				page.getLimit());
		this.setTotal(productService.getTotalByWhere("lostDate < '"
				+ DateUtils.formatDate(DateUtils.addDay(7))
				+ "' AND isDisuse = 0"));
		this.setSuccess(true);
		return SUCCESS;
	}

	public String findByUser() {
		Subject subject = SecurityUtils.getSubject();
		Integer uid = (Integer) subject.getSession().getAttribute("UID");
//		System.out.println("userId:" + uid);
		if (uid == null) {
			this.setSuccess(false);
			return SUCCESS;
		}
		listProduct = productService.findByProperty("userId", uid, page.getStart(), page.getLimit());
		
		this.setTotal(productService.getTotalByWhere("userId = " + uid));
		this.setSuccess(true);
		return SUCCESS;
	}

	public String search() {
		if (query != null && !query.equals("佛山") && !query.equals("佛山市")) {
			int type = RegexUtil.regex(query);
			if (type == 1) {
				this.setTotal(productService.getTotalBySearch("siteUrl", query));
				if (this.getTotal() < 10) {
					listProduct = productService.searchByProperty("siteUrl",
							query);
					this.setSuccess(true);
					return SUCCESS;
				} else {
					this.setMessage("请缩小搜索范围再试。");
					this.setSuccess(false);
					return SUCCESS;
				}
			}
			if (type == 2) {
				this.setTotal(productService.getTotalBySearch("text", query));
				if (this.getTotal() < 10) {
					listProduct = productService
							.searchByProperty("text", query);
					this.setSuccess(true);
					return SUCCESS;
				} else {
					this.setMessage("请缩小搜索范围再试。");
					this.setSuccess(false);
					return SUCCESS;
				}

			}
		}
		this.setSuccess(false);
		return SUCCESS;

	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public IPageAble getPage() {
		return page;
	}

	public void setPage(IPageAble page) {
		this.page = page;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public String getQuery() {
		return query;
	}

	public void setQuery(String query) {
		this.query = query.trim();
	}

	@JSON(name = "root")
	public List<BaseProduct> getListProduct() {
		return listProduct;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public void setListProduct(List<BaseProduct> listProduct) {
		this.listProduct = listProduct;
	}

}
